package genetic.q2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;

public class TSPGraph {

	String filename;
	String[] cities;
	int[][] matrix;
	int maxArc;

	public TSPGraph(String filename) {
		this.filename = filename;
	}

	public void readFile(){
		File f = new File(filename);
		try {
			BufferedReader in = new BufferedReader(new FileReader(f));
			String line = in.readLine();
			StringTokenizer st = new StringTokenizer(line, "\t");
			Vector<String> cityNames = new Vector<String>();
			st.nextToken();
			while (st.hasMoreTokens()){
				cityNames.add(st.nextToken());
			}
			cities = cityNames.toArray(new String[cityNames.size()]);
			matrix = new int[cities.length][cities.length];

			line = in.readLine();
			int row = 0;
			int col = 0;
			while (line != null && !line.equalsIgnoreCase("end")){
				st = new StringTokenizer(line, "\t");
				st.nextToken();
				col = 0;
				while (st.hasMoreTokens()){
					matrix[row][col] = Integer.parseInt(st.nextToken());
					this.maxArc = Math.max(maxArc, matrix[row][col]);
					col++;
				}
				row++;
				line = in.readLine();
			}
			maxArc = maxArc * (matrix.length+0);
			System.out.print("matrix.length= "+matrix.length);
			for (int j=0; j<matrix.length; j++){
				System.out.print(cities[j] + "\t");
			}
			System.out.println();
			for (int i=0; i<matrix.length; i++){
				System.out.print(cities[i] + "\t");
				for (int j=0; j<matrix.length; j++){
					System.out.print(matrix[i][j] + "\t");
				}
				System.out.println();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		new TSPGraph("TSP1.txt").readFile();
	}

	public int getDistance(int i, int j) {
		return matrix[i][j];
	}

	public int getMaxArc(){
		return maxArc;
	}

	public int getSize() {
		return matrix.length;
	}

}
